package cn.fxl.org.service.impl;

import cn.fxl.basic.exception.BusinessExciption;
import cn.fxl.basic.service.impl.BasicServiceImpl;
import cn.fxl.basic.utils.BaiduAiUtils;
import cn.fxl.basic.utils.Md5Utils;
import cn.fxl.basic.utils.StrUtils;
import cn.fxl.org.domain.Employee;
import cn.fxl.org.domain.Shop;
import cn.fxl.org.domain.ShopAuditLog;
import cn.fxl.org.domain.ShopDto;
import cn.fxl.org.mapper.EmployeeMapper;
import cn.fxl.org.mapper.ShopAuditLogMapper;
import cn.fxl.org.mapper.ShopMapper;
import cn.fxl.org.service.IShopService;
import cn.fxl.user.domain.Logininfo;
import cn.fxl.user.mapper.LogininfoMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;

@Service
public class ShopServiceImpl extends BasicServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
    private LogininfoMapper logininfoMapper;


    @Override
    public void update(Shop shop) {
        super.update(shop);
    }

    @Override
    public void settlement(Shop shop) {
        //获取验证项
        String name = shop.getName();
        String tel = shop.getTel();
        String address = shop.getAddress();
        String logo = shop.getLogo();
        //空值验证   空为true
        if(StringUtils.isEmpty(name) || StringUtils.isEmpty(tel) || StringUtils.isEmpty(address)){
                throw new BusinessExciption("信息不能为空");
        }
        if(StringUtils.isEmpty(logo)){
            throw new BusinessExciption("logo不能为空");
        }
        //判断是否注册过(根据shop里的信息查询出来不为null就说明注册过)
        Shop shop1=shopMapper.getNameAndAddress(shop);
        if(shop1!=null){
            throw new BusinessExciption("已经入驻过");
        }

        //百度UI智能审核
        //百度UI文本验证
        if(!BaiduAiUtils.textCensor(name)){
            throw new BusinessExciption("店铺名字违规");
        }
        //百度UI图片验证
        String img = "http://123.207.27.208"+shop.getLogo();
        if (!BaiduAiUtils.imgCensor(img)){
            throw new BusinessExciption("店铺logo违规");
        }

        //获取员工信息
        Employee admin = shop.getAdmin();
        //获取32位盐值
        String complexRandomString = StrUtils.getComplexRandomString(32);
        //获取密码
        String password = admin.getPassword();
        //设置盐值
        admin.setSalt(complexRandomString);
        //加密加盐
        String md5Psd = Md5Utils.encrypByMd5(complexRandomString + password);
        admin.setPassword(md5Psd);
        Logininfo logininfo = new Logininfo();
        //javabean属性拷贝
        BeanUtils.copyProperties(admin,logininfo);
        //设置类型为0 代表员工
        logininfo.setType(0);
        logininfoMapper.save(logininfo);
        //获取登录表自增id设置Logininfo_id
        admin.setLogininfo_id(logininfo.getId());

        //先添加员工获取自增id
        employeeMapper.save(admin);
        //员工的id为店铺的管理员id
        shop.setAdmin_id(admin.getId());

        //保存店铺信息获取自增id
        shopMapper.save(shop);
        //给员工添加店铺id
        admin.setShop_id(shop.getId());
        //重新修改员工信息
        employeeMapper.update(admin);

    }

    /**
     * 审核通过
     * @param shopAuditLog
     */
    @Override
    public void auditPass(ShopAuditLog shopAuditLog) throws MessagingException {//店铺id  note
        //通过前端传的店铺id查询店铺信息
        Shop shop = shopMapper.getOne(shopAuditLog.getShop_id());
        //将店铺状态改为2-已人工审核待激活
        shop.setState(2);

        shopMapper.update(shop);
        //设置日志信息
        shopAuditLog.setState(2);
        shopAuditLog.setAudit_id(324L);//管理员id
        shopAuditLogMapper.save(shopAuditLog);

        //邮件激活
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
        Employee one = employeeMapper.getOne(shop.getAdmin_id());
        helper.setFrom("807018277@qq.com");
        helper.setSubject("店铺激活邮件");
        helper.setText("<h1>您的店铺已注册 <a href='http://localhost:8080/shop/active/"+shop.getId()+"'>点击激活</a></h1>",true);
//        helper.addAttachment("CgAIC2LMDkWAFbwbAAEHcv-x8dg205.jpg",new File("http://123.207.27.208/group1/M00/00/73/CgAIC2LMDkWAFbwbAAEHcv-x8dg205.jpg"));
        helper.setTo(one.getEmail());
        javaMailSender.send(mimeMessage);
    }
    /**
     * 审核驳回
     * @param shopAuditLog
     */
    @Override
    public void auditReject(ShopAuditLog shopAuditLog) throws MessagingException {//店铺id  日志信息note
        //修改店铺状态
        Shop shop = shopMapper.getOne(shopAuditLog.getShop_id());
        shop.setState(4);

        shopMapper.update(shop);
        //设置日志信息
        shopAuditLog.setState(4);
        shopAuditLog.setAudit_id(324L);//管理员id
        shopAuditLogMapper.save(shopAuditLog);

        //邮件激活
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
        Employee one = employeeMapper.getOne(shop.getAdmin_id());
        helper.setFrom("807018277@qq.com");
        helper.setSubject("店铺激活邮件");
        helper.setText("<h1>入驻失败 <a href='http://localhost:8081/#/register'>点击重新入驻</a></h1>",true);
        helper.setTo(one.getEmail());
        javaMailSender.send(mimeMessage);
            //驳回删除数据库里的店铺管理员信息和店铺信息
        Long admin_id = shop.getAdmin_id();
        Employee employee = employeeMapper.getOne(admin_id);
        logininfoMapper.remove(employee.getLogininfo_id());
        employeeMapper.remove(shop.getAdmin_id());
        shopMapper.remove(shopAuditLog.getShop_id());
    }

    /**
     * 商家激活店铺
     * @param id
     */
    @Override
    public void activeShop(Long id) {//shopid
        Shop shop = shopMapper.getOne(id);
        shop.setState(3);
        shopMapper.update(shop);
        ShopAuditLog shopAuditLog = new ShopAuditLog();
        shopAuditLog.setState(3);
        shopAuditLog.setShop_id(id);
        shopAuditLog.setAudit_id(shop.getAdmin_id());
        shopAuditLogMapper.save(shopAuditLog);
    }

    @Override
    public List<ShopDto> getEcharts() {
        return shopMapper.getEcharts();
    }
}
